package com.module.user.ui.login

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.CommonMessageRelayModel
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyPre
import com.common.app.data.bean.user.LoginBean
import com.common.app.helper.RouterHelper
import com.common.app.helper.UserHelper
import com.common.app.utls.Constants
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.getShowMsg
import com.common.base.app.extras.isNoEmpty
import com.common.base.app.extras.no
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.SoftInputUtils
import com.common.base.utils.SpUtilsManagement
import com.common.base.utils.SpanUtils
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ToastUtils
import com.common.base.utils.ViewUtils
import com.module.user.R
import com.module.user.databinding.UserActivityVerificationCodeIputBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/9/2
 * <p>
 * 描述：验证码输入页面
 * <p>
 * 修订历史：
 */
class VerificationCodeInputActivity : AppActivity<UserActivityVerificationCodeIputBinding, LoginViewModel>(),View.OnClickListener {

    override fun getViewModel() = LoginViewModel::class.java

    override fun getViewContentBinding() = UserActivityVerificationCodeIputBinding.inflate(layoutInflater)

    override fun isStatusBarTranslate(): Boolean = true
    override fun isShowToolBar(): Boolean = false

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var commonMessageRelayModel: CommonMessageRelayModel? = null

    @Autowired(name = KeyBundle.EXPAND_DATA)
    @JvmField
    var mPhone: String? = ""

    var mCountry: String = "+86"

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle("")
        headTitleUtils.setLineStyle(10)

        SpanUtils.create()
            .addSection(getString(R.string.user_ver_code_send_splice))
            .addForeColorSection("  $mPhone",
                ViewUtils.getColor(R.color.text_content)
            )
            .showIn(mViewContentBinding.tvSendSplice)
    }

    override fun lazyInitData() {
        super.lazyInitData()

        mPhone.isNullOrEmpty().no {
            mViewContentBinding.tvSmsCode.startCountDown()
            SoftInputUtils.setSoftInput(mViewContentBinding.ibView, true)
            mViewContentBinding.ibView.showKeyboard()
        }

    }


    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.tvSmsCode.setOnClickListener(onClick = this)
        mViewContentBinding.ibView.inputChangeContent {
                //输入一个回调一下
            }
        mViewContentBinding.ibView.inputEndResult{
            //最终的结果值
            toLogin(it)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.tvSmsCode -> {
                if (mPhone.isNullOrEmpty()) {
                    showToast(R.string.common_please_input_phone)
                }else{
                    Constants.curPhone = mPhone!!
                    requestTransTer()
                }
            }
        }
    }

    /**
     * 获取验证码
     */
    private fun requestTransTer() {
        showDialogLoading()
        mViewModel.requestVerCode(mPhone?:"", 4, mCountry)
            .observe(this) {
                dismissLoadingDialog()
                it.isSuccess().yes {
                    showToast("验证码发送成功！")
                    mViewContentBinding.tvSmsCode.startCountDown()
                }.otherwise {
                    showToast(it.Message)
                }
            }
    }

    private fun toLogin(verCode:String?) {

        if (mPhone.isNullOrEmpty()) {
            ToastUtils.showToast(R.string.common_please_input_phone)
            return
        }

        //验证格式
        if ((mPhone?.length?:0) < 5){
            showToast(R.string.common_wrong_phone_number)
            return
        }

        if (verCode.isNullOrEmpty()) {
            ToastUtils.showToast(R.string.common_input_verification_code)
            return
        }
        mPhone?.let {mPhoneIt->
            showDialogLoading()
            mViewContentBinding.tvSmsCode.text = getString(R.string.user_login_ac_under_ver)
            mViewModel.login(mCountry, mPhoneIt, verCode, 1) {
                dismissLoadingDialog()
                showToast(it.getShowMsg())
                mViewContentBinding.tvSmsCode.text = getString(R.string.common_re_send_hint)
            }.observe(this) {
                it.isSuccess().yes {
                    Constants.curPhone = mPhoneIt
                    UserHelper.loginSuccess(it)
                    SpUtilsManagement.putBoolean(KeyPre.KEY_USER_AGREEMENT, true)
                    SpUtilsManagement.putString(KeyPre.KEY_USER_PHONE, mPhoneIt)
                    SpUtilsManagement.putString(KeyPre.KEY_USER_PHONE_CODE, mCountry)
                    dismissLoadingDialog()
                    SoftInputUtils.hideSoftInput(this@VerificationCodeInputActivity)
                    RouterHelper.navigationPostcard(RouterHub.ROUTER_DOWN_CLOUD_DATA_ACTIVITY)
                        .withParcelable(KeyBundle.BUNDLE_KEY, commonMessageRelayModel)
                        .navigation().run {
                            finish()
                        }
                }.otherwise {
                    mViewContentBinding.tvSmsCode.text = getString(R.string.common_re_send_hint)
                    dismissLoadingDialog()
                    it.Message.isNoEmpty().yes { showToast(it.Message) }
                }
            }
        }
    }

}